package com.sundaying.gooway.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.sundaying.gooway.dao.TopItemDao;
import com.sundaying.gooway.entity.TopItem;
import com.sundaying.gooway.entity.UrlClass;
import com.sundaying.gooway.entity.UrlItem;
import com.sundaying.gooway.vo.ClassUrlItem;

@Component
public class TopItemService {
	private TopItemDao topItemDao;

	public TopItemDao getTopItemDao() {
		return topItemDao;
	}

	@Resource
	public void setTopItemDao(TopItemDao topItemDao) {
		this.topItemDao = topItemDao;
	}

	public int add(TopItem obj) {
		return topItemDao.save(obj);
	}

	public int update(TopItem obj) {
		return topItemDao.update(obj);
	}

	public int delete(long id) {
		return topItemDao.delete(topItemDao.load(id));
	}

	public List<TopItem> list() {
		return topItemDao.list();
	}

	public List<TopItem> list(int page, int rows, String sort, String order,
			TopItem topItem) {
		String hql = "from TopItem topItem where 1=1 ";
		List<Object> param = new ArrayList<Object>();
		hql = addWhere(hql, topItem, param);
		// 排序
		if (!sort.equals("")) {
			hql += " order by " + sort;

			if (!order.toLowerCase().equals("asc")) {
				hql += " desc";
			}
		}

		List<TopItem> topItemList = topItemDao.list(hql, param, page, rows);
		return topItemList;
	}

	public long total(TopItem topItem) {
		long result = 0;
		String hql = "select count(*) from UrlItem urlItem where 1=1 ";
		List<Object> param = new ArrayList<Object>();
		hql = addWhere(hql, topItem, param);
		result = topItemDao.count(hql, param);
		return result;
	}

	public List<TopItem> getTopList(int classId, int rows) {
		String hql = "from TopItem topItem where and topItem.classId=? order by topItem.orderNum desc";
		List<Object> param = new ArrayList<Object>();
		param.add(classId);
		List<TopItem> topItemList = topItemDao.list(hql, param);
		return topItemList;
	}
	
	public List<UrlClass> getTopUrlClassList(int classId, int rows){
		String hql = "select b from TopItem a, UrlClass b where a.itemId=b.id and a.classId=? order by a.orderNum desc";
		List<Object> param = new ArrayList<Object>();
		param.add(classId);
		List<UrlClass> topItemList = topItemDao.getUrlClassList(hql, param, rows);
		return topItemList;
	}
	
	public List<UrlItem> getTopUrlItemList(int classId, int rows){
		String hql = "select b from TopItem a, UrlItem b where a.itemId=b.id and a.classId=? order by a.orderNum desc";
		List<Object> param = new ArrayList<Object>();
		param.add(classId);
		List<UrlItem> topItemList = topItemDao.getUrlItemList(hql, param, rows);
		return topItemList;
	}
	
	public List<UrlItem> getTopUrlItemList(int classId, int rows, int urlClassId){
		String hql = "select b from TopItem a, UrlItem b where a.itemId=b.id and a.classId=? and b.classId=? order by a.orderNum desc";
		List<Object> param = new ArrayList<Object>();
		param.add(classId);
		param.add(urlClassId);
		List<UrlItem> topItemList = topItemDao.getUrlItemList(hql, param, rows);
		return topItemList;
	}

	private String addWhere(String hql, TopItem topItem, List<Object> param) {
		if (topItem.getClassId() > 0) {
			hql += " and topItem.classId=? ";
			param.add(topItem.getClassId());
		}
		return hql;
	}
}
